﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;


// The Basic Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234237

namespace StudyTime_1
{
    /// <summary>
    /// A basic page that provides characteristics common to most applications.
    /// </summary>
    /// 

        

    public sealed partial class editSelected : StudyTime_1.Common.LayoutAwarePage
    {
        private int counter = 0;
        private int backCounter = 0;
        private bool wentBack = false;
        public static bool goHome = false;
        private byte[] img;
        private static List<quizQuest> tempQuizList = new List<quizQuest>();
        private static List<quizQuest> quizMigration = new List<quizQuest>();
        private static List<studyCard> tempCardList = new List<studyCard>();
        private static List<studyCard> cardMigration = new List<studyCard>();

        public editSelected()
        {
            this.InitializeComponent();
            qAnswer.ItemsSource = new List<string> { "A", "B", "C", "D" };
            
            
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
        }

        /// <summary>
        /// Preserves state associated with this page in case the application is suspended or the
        /// page is discarded from the navigation cache.  Values must conform to the serialization
        /// requirements of <see cref="SuspensionManager.SessionState"/>.
        /// </summary>
        /// <param name="pageState">An empty dictionary to be populated with serializable state.</param>
        protected override void SaveState(Dictionary<String, Object> pageState)
        {
        }

       async private void addCard_Click(object sender, RoutedEventArgs e)
        {
            await setCard();
        }

        private void pageRoot_Loaded(object sender, RoutedEventArgs e)
        {
            counter = 0;

           

            if (App.setManageType == true)
            {
                cardEditContainer.Visibility = Visibility.Visible;
                quizEditContainer.Visibility = Visibility.Collapsed;
                

               // SolidColorBrush b =  new SolidColorBrush(Color.FromArgb(0xFF,0x25,0x9C,0xFF));
                
                mainGrid.Background = new SolidColorBrush(Color.FromArgb(0xFF,0x1B,0x58,0xB8));
                setCardText(0);
            }
            else
            {
                cardEditContainer.Visibility = Visibility.Collapsed;
                quizEditContainer.Visibility = Visibility.Visible;
                mainGrid.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x12, 0x84, 0x25));
                setText(0);
                if (App.editQuizQuest.Count > 1)
                {

                    nextQuestion.IsEnabled = true;
                    lstQuestion.IsEnabled = true;
                }
            }
        }



        private void setCard(int i)
        {
            question.Text = App.editStudyCard[i].question;
            answer.Text = App.editStudyCard[i].answer;

            if (App.editStudyCard.Count > 1)
            {


            }
            if (counter < App.editStudyCard.Count - 1)
            {
                counter++;
            }



        }



      


        async private void setNextCard(bool b)
        {
            if (wentBack == true)
            {
                if (b == true)
                {
                    if (App.editStudyCard.Count > 1)
                    {

                        nextQuestion.IsEnabled = true;
                        lstQuestion.IsEnabled = true;
                    }

                    if (backCounter < App.editStudyCard.Count)
                    {
                        if (backCounter < 0)
                        {
                            backCounter = 0;
                        }

                        //App.quizList.Remove(App.editStudyCard[backCounter]);
                        //App.editStudyCard.RemoveAt(backCounter);
                        //await setQuizQuest();

                        if (backCounter == App.editStudyCard.Count - 1)
                        {
                            qError.Text = "You have Edited all of your selected questions";
                            backCounter = App.editStudyCard.Count;

                        }
                    }
                    else
                    {
                        qError.Text = "";
                        backCounter++;
                        setCardText(backCounter);
                    }
                }
                if (b == false)
                {

                    if (App.editStudyCard.Count > 1)
                    {

                        nextQuestion.IsEnabled = true;
                        lstQuestion.IsEnabled = true;
                    }
                    if (backCounter > -1)
                    {

                        backCounter--;
                        setCardText(backCounter);

                    }

                    if (backCounter == -1)
                    {
                        qError.Text = "You are at the beginning of your selected Items.";
                    }
                }

            }
            else
            {
                if (b == true)
                {
                    if (App.editStudyCard.Count > 1)
                    {

                        nextQuestion.IsEnabled = true;
                        lstQuestion.IsEnabled = true;
                    }

                    if (counter < App.editStudyCard.Count)
                    {
                        if (counter < 0)
                        {
                            counter = 0;
                        }

                        if (counter >= App.editStudyCard.Count - 1)
                        {


                            qError.Text = "You have Edited all of your selected questions";
                            counter = App.editStudyCard.Count;
                            await classes.messages.setMssg("finishEditQuiz");

                            if (goHome == true)
                            {
                                this.Frame.Navigate(typeof(Page1));
                            }

                        }
                        else
                        {
                            //App.quizList.Remove(App.editStudyCard[counter]);

                            //await setQuizQuest();

                            qError.Text = "";
                            counter++;
                            setCardText(counter);
                        }
                    }
                }


            }
            if (b == false)
            {

                if (App.editStudyCard.Count > 1)
                {

                    nextQuestion.IsEnabled = true;
                    lstQuestion.IsEnabled = true;
                }
                if (counter > -1)
                {

                    counter--;
                    setCardText(counter);

                }

                if (counter == -1)
                {
                    qError.Text = "You are at the beginning of your selected Items.";
                }




            }

        }

        //private void setCard(int i)
        //{
        //    question.Text = App.editStudyCard[i].question;
        //    answer.Text = App.editStudyCard[i].answer;

        //    if (App.editStudyCard.Count > 1)
        //    {

                
        //    }
        //    if (counter < App.editStudyCard.Count - 1)
        //    {
        //        counter++;
        //    }

           
            
        //}

       

       async private void setText(int i)
        {
            if (wentBack == true)
            {
                answerA.Text = App.editQuizQuest[i].answerA;
                answerB.Text = App.editQuizQuest[i].answerB;
                answerC.Text = App.editQuizQuest[i].answerC;
                answerD.Text = App.editQuizQuest[i].answerD;
                multQuest.Text = App.editQuizQuest[i].question;

                await classes.getImg.convertToPic(App.editQuizQuest[i].picture);
                qPreview.Source = classes.getImg.getPicture();
            }
            else
            {
                qAnswer.SelectedItem = App.editQuizQuest[i].answer;

                answerA.Text = App.editQuizQuest[i].answerA;
                answerB.Text = App.editQuizQuest[i].answerB;
                answerC.Text = App.editQuizQuest[i].answerC;
                answerD.Text = App.editQuizQuest[i].answerD;
                multQuest.Text = App.editQuizQuest[i].question;

                await classes.getImg.convertToPic(App.editQuizQuest[i].picture);

                qPreview.Source = classes.getImg.getPicture();
            }
        }


       async private void setCardText(int i)
       {
           if (wentBack == true)
           {
               answer.Text = App.editStudyCard[i].answer;
               question.Text = App.editStudyCard[i].question;

               await classes.getImg.convertToPic(App.editStudyCard[i].picture);
               insertPreview.Source = classes.getImg.getPicture();
           }
           else
           {
             
               answer.Text = App.editStudyCard[i].answer;
               question.Text = App.editStudyCard[i].question;

               await classes.getImg.convertToPic(App.editStudyCard[i].picture);
               insertPreview.Source = classes.getImg.getPicture();
           }
       }


    

         async private void setQuiz(bool b)
        {
            if (wentBack == true)
            {
               if (b == true)
                {
                    if (App.editQuizQuest.Count > 1)
                    {

                        nextQuestion.IsEnabled = true;
                        lstQuestion.IsEnabled = true;
                    }

                    if (backCounter < App.editQuizQuest.Count)
                    {
                        if (backCounter < 0)
                        {
                            backCounter = 0;
                        }

                        //App.quizList.Remove(App.editQuizQuest[backCounter]);
                        //App.editQuizQuest.RemoveAt(backCounter);
                        //await setQuizQuest();

                        if (backCounter == App.editQuizQuest.Count - 1)
                        {
                            qError.Text = "You have Edited all of your selected questions";
                            backCounter = App.editQuizQuest.Count;

                        }
                    }
                    else
                    {
                        qError.Text = "";
                        backCounter++;
                        setText(backCounter);
                    }
                }
               if (b == false)
               {

                   if (App.editQuizQuest.Count > 1)
                   {

                       nextQuestion.IsEnabled = true;
                       lstQuestion.IsEnabled = true;
                   }
                   if (backCounter > -1)
                   {

                       backCounter--;
                       setText(backCounter);

                   }

                   if (backCounter == -1)
                   {
                       qError.Text = "You are at the beginning of your selected Items.";
                   }
               }

            }
            else
            {
                if (b == true)
                {
                    if (App.editQuizQuest.Count > 1)
                    {

                        nextQuestion.IsEnabled = true;
                        lstQuestion.IsEnabled = true;
                    }

                    if (counter < App.editQuizQuest.Count)
                    {
                        if (counter < 0)
                        {
                            counter = 0;
                        }

                        if (counter >= App.editQuizQuest.Count - 1)
                        {


                            qError.Text = "You have Edited all of your selected questions";
                            counter = App.editQuizQuest.Count;
                            await classes.messages.setMssg("finishEditQuiz");

                            if (goHome == true)
                            {
                                this.Frame.Navigate(typeof(Page1));
                            }

                        }
                        else
                        {
                            //App.quizList.Remove(App.editQuizQuest[counter]);

                            //await setQuizQuest();

                            qError.Text = "";
                            counter++;
                            setText(counter);
                        }
                    }
                }

                
            }
            if (b == false)
            {

                if (App.editQuizQuest.Count > 1)
                {

                    nextQuestion.IsEnabled = true;
                    lstQuestion.IsEnabled = true;
                }
                if (counter > -1)
                {

                        counter--;
                        setText(counter);
                    
                }

                if (counter == -1)
                    {
                        qError.Text = "You are at the beginning of your selected Items.";
                    }
                  

               

            }

        }

        async private void addImage_Click(object sender, RoutedEventArgs e)
        {
            await classes.getImg.getImage();
            App.editStudyCard[counter].picture = classes.getImg.getArray();
            insertPreview.Source = classes.getImg.getPicture();
        }

        private void getSetImage(bool b)
        {

        }


        async private Task setCard()
        {
            if (counter > -1)
            {
                studyCard origCard = App.editStudyCard[counter];
                studyCard temp = new studyCard();

                temp.answer = answer.Text;
                temp.question = question.Text;

                temp.picture = classes.getImg.getArray();

               cardMigration.Clear();
                foreach (studyCard card in App.studyCardList)
                {
                    if (card != origCard)
                    {
                        cardMigration.Add(card);

                    }
                    else
                    {
                        cardMigration.Add(temp);
                    }
                }

                tempCardList.Clear();
                foreach (studyCard card2 in App.editStudyCard)
                {
                    if (card2 != origCard)
                    {
                        tempCardList.Add(card2);
                    }
                    else
                    {
                        tempCardList.Add(temp);
                    }
                }

                App.studyCardList.Clear();
                foreach (studyCard card3 in cardMigration)
                {
                    App.studyCardList.Add(card3);
                }

                App.editStudyCard.Clear();
                foreach (studyCard card4 in tempCardList)
                {
                    App.editStudyCard.Add(card4);
                }

                //App.quizList.Clear();
                //App.quizList = quizMigration;
                //App.quizList.Add(temp);
                //App.editQuizQuest.Remove(origQuest);
                //App.editQuizQuest.Add(temp);
                //App.editQuizQuest.Clear();
                //App.editQuizQuest = tempQuizList;
                backCounter = App.editStudyCard.Count() - 1;

                await classes.serializer.updateList(true);
                
            }
        }

       async private Task setQuizQuest()
        {
            if (counter > -1)
            {
                quizQuest origQuest = App.editQuizQuest[counter];
                quizQuest temp = new quizQuest();
                temp.answer = qAnswer.SelectedItem.ToString();
                temp.answerA = answerA.Text;
                temp.answerB = answerB.Text;
                temp.answerC = answerC.Text;
                temp.answerD = answerD.Text;
                temp.question = multQuest.Text;

                temp.picture = classes.getImg.getArray();

                quizMigration.Clear();
                foreach (quizQuest quiz in App.quizList)
                {
                    if (quiz != origQuest)
                    {
                        quizMigration.Add(quiz);

                    }
                    else
                    {
                        quizMigration.Add(temp);
                    }
                }

                tempQuizList.Clear();
                foreach (quizQuest quiz2 in App.editQuizQuest)
                {
                    if (quiz2 != origQuest)
                    {
                        tempQuizList.Add(quiz2);
                    }
                    else
                    {
                        tempQuizList.Add(temp);
                    }
                }

                App.quizList.Clear();
                foreach (quizQuest quiz3 in quizMigration)
                {
                    App.quizList.Add(quiz3);
                }

                App.editQuizQuest.Clear();
                foreach (quizQuest quiz4 in tempQuizList)
                {
                    App.editQuizQuest.Add(quiz4);
                }

                //App.quizList.Clear();
                //App.quizList = quizMigration;
                //App.quizList.Add(temp);
                //App.editQuizQuest.Remove(origQuest);
                //App.editQuizQuest.Add(temp);
                //App.editQuizQuest.Clear();
                //App.editQuizQuest = tempQuizList;
                backCounter = App.editQuizQuest.Count() - 1;

                await classes.serializer.updateList(false);
            }


        }

        async private void setNewQuestion_Click(object sender, RoutedEventArgs e)
        {

            await setQuizQuest();

        }

       async private void nextQuestion_Click(object sender, RoutedEventArgs e)
        {
            await setQuizQuest();
            setQuiz(true);
        }

      async private void lstQuestion_Click(object sender, RoutedEventArgs e)
       {
          
            counter--;
            backCounter--;
            //wentBack = true;

           if (counter > -1)
           { 
               setText(counter);
               await setQuizQuest();
              // await setQuiz(false);
             
               qError.Text = "";
           }
           if (counter == -1)
                    {
                        qError.Text = "You are at the beginning of your selected Items.";
                        backCounter = 0;
                        counter = 0;
                    }
                  
          
          
       }

       async private void qaddImage_Click(object sender, RoutedEventArgs e)
       {
          await classes.getImg.getImage();
           App.editQuizQuest[counter].picture = classes.getImg.getArray();
           qPreview.Source = classes.getImg.getPicture();
       }

      async private void nextItem_Click(object sender, RoutedEventArgs e)
       {
           await setCard();
           setNextCard(true);
       }

      private void rmvAd_Click(object sender, RoutedEventArgs e)
      {
          removeAds.setNofication();
      }
       
    }
}
